package de.dhbw.karlsruhe.picsim.model.decode;

import de.dhbw.karlsruhe.picsim.model.commands.PicInstruction;
import de.dhbw.karlsruhe.picsim.model.commands.impl.CLRWDT;
import de.dhbw.karlsruhe.picsim.model.commands.impl.RETFIE;
import de.dhbw.karlsruhe.picsim.model.commands.impl.Return;
import de.dhbw.karlsruhe.picsim.model.commands.impl.ReturnWithLiteralInW;
import de.dhbw.karlsruhe.picsim.model.memory.OpCode;
import de.dhbw.karlsruhe.picsim.util.Bits;
import de.dhbw.karlsruhe.picsim.util.impl.BitUtilities;
import de.dhbw.karlsruhe.picsim.util.impl.Lengths;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jetbrains.annotations.NotNull;

/**
 * Created by IntelliJ IDEA.
 * User: goesen
 * Date: Apr 18, 2010
 * Time: 1:06:11 PM
 */
final class LiteralDecoder00 implements Decoder {
    private static final Log log = LogFactory.getLog(LiteralDecoder00.class);

    @NotNull
    @Override
    public PicInstruction decode(@NotNull final OpCode opCode) {
        log.debug("now decoding opcode" + opCode);

        final Bits bits = opCode.getValue();

        if (BitUtilities.fromLeastSignificantBitFirst(bits) == 8) {
            return new Return();
        } else if (bits.isSet(11) && bits.isSet(10) && !bits.isSet(9) && bits.isSet(8)) {
            return new ReturnWithLiteralInW(BitUtilities.subBits(bits, 0, Lengths.FILE_LENGTH));
        } else if (bits.isSet(0) && bits.isSet(3)) {
            return new RETFIE();
        } else if (bits.isSet(2)) {
            return new CLRWDT();
        }

        throw new UnsupportedOperationException();
    }
}